package org.hive.vmware.translator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hive.vmware.client.model.VmCenterServerVO;
import org.hive.vmware.client.model.VmGroupVO;
import org.hive.vmware.client.model.VmVO;
import org.hive.vmware.model.VmBee;
import org.hive.vmware.model.VmCenterServer;
import org.hive.vmware.model.VmGroupBee;

public class VmCenterServerModelVOTranslator {

	public VmCenterServerVO translateVmCenterServerToVO(VmCenterServer vmCenterServer) {
		if (vmCenterServer != null) {
			VmCenterServerVO vmCenterServerVO = new VmCenterServerVO();
			vmCenterServerVO.setUrl(vmCenterServer.getId());
			vmCenterServerVO.setUserId(vmCenterServer.getUserId());
			vmCenterServerVO.setPassword(vmCenterServer.getPassword());
			return vmCenterServerVO;
		} else {
			return null;
		}
	}
	
	public VmCenterServer translateVmCenterServerVOToModel(VmCenterServerVO vmCenterServerVO) {
		if (vmCenterServerVO != null) {
			VmCenterServer vmCenterServer = new VmCenterServer();
			vmCenterServer.setId(vmCenterServerVO.getUrl());
			vmCenterServer.setUserId(vmCenterServerVO.getUserId());
			vmCenterServer.setPassword(vmCenterServerVO.getPassword());
			return vmCenterServer;
		} else {
			return null;
		}
	}
	
	public List<VmGroupVO> translateVmGroupsToVO(List<VmGroupBee> vmgroupBees){
		List<VmGroupVO> result = new ArrayList<VmGroupVO>();
		for (VmGroupBee vmGroupBee : vmgroupBees) {
			result.add(translateVmGroupToVO(vmGroupBee));
		}
		return result; 
	}
	
	VmGroupVO translateVmGroupToVO(VmGroupBee vmGroupBee) {
		VmGroupVO vmGroupVO = new VmGroupVO();
		vmGroupVO.setName(vmGroupBee.getId());
		List<VmVO> vmVOs = new ArrayList<VmVO>();
		Set<VmBee> vms = vmGroupBee.getVms();
		Iterator<VmBee> vmBeeIterator = vms.iterator();
		while(vmBeeIterator.hasNext()) {
			VmVO vmVO = translateVmToVO(vmBeeIterator.next());
			vmVO.setVmGroupVO(vmGroupVO);
			vmVOs.add(vmVO);
		}
		
		vmGroupVO.setVms(vmVOs);
		return vmGroupVO;
	}
	
	VmVO translateVmToVO(VmBee vm) {
		VmVO vmVO = new VmVO();
		vmVO.setName(vm.getName());
		vmVO.setCenterServer(vm.getCenterServer());
		vmVO.setDataCenter(vm.getDataCenter());
		vmVO.setHost(vm.getHost());
		return vmVO;
	}
	
	
}
